package com.nayun.gjh.xiaokabanghou.common;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class SafetyRequestUtil {
    private static final ConcurrentHashMap<String, AtomicInteger> requestCountMap = new ConcurrentHashMap<>();

    private static boolean isRequestAllowed(String ipAddress,int maxRequestsPreSecond) {
        requestCountMap.putIfAbsent(ipAddress, new AtomicInteger(0));
        AtomicInteger count = requestCountMap.get(ipAddress);

        // 原子操作递增计数器
        int currentCount = count.incrementAndGet();

        // 判断是否超过限制
        return currentCount <= maxRequestsPreSecond;
    }
}